package com.sh.sales.happysales.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.utils.LoginOutUtil;
import com.sh.sales.happysales.app.utils.StatusBarCompat;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.base.BaseMessageEvent;
import com.sh.sales.happysales.ui.fragment.MineFragment;
import com.sh.sales.happysales.ui.fragment.ProfitFragment;
import com.sh.sales.happysales.webconfig.EventType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhouyucheng on 2017/9/15.
 */

public class ContentActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.bottomView)
    LinearLayout bottomView;
    @BindView(android.R.id.tabhost)
    FragmentTabHost tabhost;
    private ImageView imageViewa, imageViewb;
    private TextView tv1, tv2;
    private ImageView[] button = new ImageView[2];
    private TextView[] text = new TextView[2];
    private int[][] imageid = {{R.drawable.icon_profit_press, R.drawable.icon_profit},
            {R.drawable.icon_mine_press, R.drawable.icon_mine}
    };
    private Class[] classes = {ProfitFragment.class, MineFragment.class};
    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;

    // 定义一个布局
    private LayoutInflater layoutInflater;

    private Unbinder unbinder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        StatusBarCompat.compat(this, getResources().getColor(R.color.start_color));
        setContentView(R.layout.activity_content);
        unbinder = ButterKnife.bind(this);
        mAppManager.addActivity(this);
        initviews();
        init();
    }

    private void init() {
        layoutInflater = LayoutInflater.from(this);

        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        // 得到fragment的个数
        int count = classes.length;
        for (int i = 0; i < count; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(i + "").setIndicator(i + "");
            mTabHost.addTab(tabSpec, classes[i], null);
        }
        mTabHost.setCurrentTab(0);
        //注册事件
        EventBus.getDefault().register(this);
    }

    private void initviews() {

        imageViewa = (ImageView) findViewById(R.id.imageViewa);
        imageViewb = (ImageView) findViewById(R.id.imageViewb);
        button[0] = imageViewa;
        button[1] = imageViewb;

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        text[0] = tv1;
        text[1] = tv2;

        imageViewa.setOnClickListener(this);
        imageViewb.setOnClickListener(this);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageViewa:
            case R.id.tv1:
                changebutton(0);
                mTabHost.setCurrentTab(0);
                break;
            case R.id.imageViewb:
            case R.id.tv2:
                changebutton(1);
                mTabHost.setCurrentTab(1);
                break;
            default:
                break;
        }
    }

    private void changebutton(int m) {
        for (int i = 0; i < 2; i++) {
            if (m == i) {
                button[i].setImageResource(imageid[i][0]);
                text[i].setTextColor(getResources().getColor(R.color.standardred));
            } else {
                button[i].setImageResource(imageid[i][1]);
                text[i].setTextColor(getResources()
                        .getColor(R.color.sixsix));
            }
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            LoginOutUtil.appDoublebtExit(ContentActivity.this);
            return true;
        }
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 2)
    public void onJsEvent(BaseMessageEvent messageEvent) {
        if (messageEvent.getType() == EventType.GoBack) {
            bottomView.setVisibility(View.VISIBLE);
            Log.i("zee", "高度:" + bottomView.getHeight());
        } else if (messageEvent.getType() == EventType.GoToRank) {
            bottomView.setVisibility(View.GONE);
            Log.i("zee", "高度:" + bottomView.getHeight());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }
}
